//
//  PopupView.m
//  PurchasingMeeting
//
//  Created by zhuang yihang on 7/20/13.
//  Copyright (c) 2013 YX. All rights reserved.
//

#import "PopupView.h"

@interface PopupView(){
    int highlight_;
    
    UIView *container_;
    UIView *intervalView_;
    
    CGRect originalFrame_;
}

@end

@implementation PopupView
@synthesize delegate = delegate_;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame withItems:(NSMutableArray *)items withHighlight:(int)highlight width:(int)width tag:(int)tag{
    self = [self initWithFrame:CGRectMake(width-35, 40, 1024, 768)];
    if (self) {
        btntag = tag;
        UIView *mask = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
        [self addSubview:mask];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
        [mask addGestureRecognizer:tap];
    
        float width = 120;
        float height = 40;
        
        container_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
        [self addSubview:container_];
        
        float bottom = 0;
        intervalView_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
        [container_ addSubview:intervalView_];
        for (int i = 0; i < items.count; i++) {
            UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
            b.frame = CGRectMake(0, 0, width, height);
            b.titleLabel.font = [UIFont systemFontOfSize:14];
            b.titleLabel.textAlignment = NSTextAlignmentCenter;
            [b setTitle:[items objectAtIndex:i] forState:UIControlStateNormal];
            [b setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [intervalView_ addSubview:b];
            b.top = i * (b.height);
            [b addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            b.tag = i+10;
            bottom = b.bottom;
            if(i!=items.count-1)
            {
                UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 1)];
                line.backgroundColor = [UIColor grayColor];
                line.bottom = b.bottom;
                [intervalView_ addSubview:line];
            }
        }
        
        container_.height = bottom;
        intervalView_.height = bottom;
        intervalView_.top = 0;
        
        container_.backgroundColor = [UIColor colorWithRed:192/255.0 green:192/255.0 blue:192/255.0 alpha:1];
        
        originalFrame_ = frame;
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)click:(UIButton *)sender{
    UIButton *btn = (UIButton *)[intervalView_ viewWithTag:sender.tag];
    [btn setBackgroundColor:[UIColor whiteColor]];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [delegate_ popupViewClicked:self atIndex:sender.tag-10];
    [self dismiss];
}

- (void)animation{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDisappear)];
    container_.frame = originalFrame_;
    [UIView commitAnimations];
}

- (void)dismiss{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDisappear)];
   // container_.frame = originalFrame_;
    [UIView commitAnimations];
    NSString *tag = [NSString stringWithFormat:@"%d",btntag];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"dismiss1" object:tag];
}

- (void)animationDisappear{
    [self removeFromSuperview];
}

@end
